package com.lzx.applib.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.Date;

import static android.R.attr.max;


public class DateSelecter extends LinearLayout {

    public NumberPicker yearPicker, monthPicker, dayPicker;

    private int currentYear = 1900;
    private int currentMonth = 1;
    private int currentDay = 1;

    public DateSelecter(Context context) {
        this(context, null);
    }

    public DateSelecter(Context context, AttributeSet attrs) {
        super(context, attrs);

        setWeightSum(3);
        setOrientation(HORIZONTAL);

        yearPicker = new NumberPicker(getContext());
        monthPicker = new NumberPicker(getContext());
        dayPicker = new NumberPicker(getContext());

        initPicker(yearPicker);
        initPicker(monthPicker);
        initPicker(dayPicker);

        resetYear();
    }

    private void initPicker(NumberPicker picker) {
        LayoutParams lp = new LayoutParams(1, LayoutParams.MATCH_PARENT);
        lp.weight = 1;

        picker.setOnValueChangedListener(onValueChangeListener);
        addView(picker, lp);
    }

    private void resetYear() {
        int min = 1900;
        int max = Math.min(Calendar.getInstance().get(Calendar.YEAR), 2016);
        initPicker(yearPicker, min, max);
        yearPicker.setValue(currentYear);
        resetMonth(currentYear);
    }

    private void resetMonth(int year) {
//        currentMonth = 1;
        initPicker(monthPicker, 1, 12);
        resetDay(year, currentMonth);
        monthPicker.setValue(currentMonth);
    }

    private void resetDay(int year, int month) {
//        currentDay = 1;
        initPicker(dayPicker, 1, getDayOfMonth(year, month));
        currentDay = Math.min(currentDay, getDayOfMonth(year, month));
        dayPicker.setValue(currentDay);
    }

    OnDateChangeListener dateChangeListener;

    public String getSelectedDay() {
        String result = currentYear + "-" +currentMonth + "-" + currentDay;
        return result;
    }

    public void setDateChangeListener(OnDateChangeListener dateChangeListener) {
        this.dateChangeListener = dateChangeListener;
    }

    public void init(int year, int monthOfYear, int dayOfMonth, OnDateChangeListener onDateChangedListener) {
        currentYear = year;
        currentMonth = monthOfYear;
        currentDay = dayOfMonth;
        resetYear();
        dateChangeListener = onDateChangedListener;
    }

    private OnValueChangeListener onValueChangeListener = new OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            if (oldVal == newVal) return;
            String oldDate = getSelectedDay();
            if (picker == yearPicker) {
                resetMonth(newVal);
                currentYear = newVal;
            } else if (picker == monthPicker) {
                resetDay(currentYear, newVal);
                currentMonth = newVal;
            } else if (picker == dayPicker) {
                currentDay = newVal;
            }
            String newDate = getSelectedDay();

            if (dateChangeListener != null) {
                dateChangeListener.onDateChange(oldDate, newDate);
            }
        }
    };

    public void initPicker(NumberPicker picker, int min, int max) {
        picker.setMaxValue(0);
        picker.setMinValue(0);

        picker.setMaxValue(max);
        picker.setMinValue(min);
        picker.setValue(min);
        picker.setWrapSelectorWheel(false);
        picker.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
    }

    public static void setDividerColorAndHeight(NumberPicker picker, int color, int height) {
        try {
            Field field1 = NumberPicker.class.getDeclaredField("mSelectionDivider");
            Field field2 = NumberPicker.class.getDeclaredField("mSelectionDividerHeight");

            field1.setAccessible(true);
            field2.setAccessible(true);

            ColorDrawable colorDrawable = new ColorDrawable(color);
            field1.set(picker, colorDrawable);
            field2.set(picker, height);

        } catch (Exception e) {
        }
    }

    public static int getDayOfMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month-1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public interface OnDateChangeListener {
        void onDateChange(String oldDate, String newDate);
    }
}
